package tools.zip;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;

public class ZipString {
	
	public static byte[] compData(String str)
	{
		byte[] ret = null;
		Deflater d = new Deflater();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DeflaterOutputStream dout = new DeflaterOutputStream(baos, d);
		
		try {
			dout.write(str.getBytes());
			dout.flush();
			dout.close();			
			ret = baos.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return ret;
	}
	
	
	public static String deCompData(byte[] compData){
		ByteArrayInputStream bais = new ByteArrayInputStream(compData);
		InflaterInputStream iin = new InflaterInputStream(bais);
		ByteArrayOutputStream bout = new ByteArrayOutputStream(512);
		
		int b;
		try {
			while((b = iin.read()) != -1){
				bout.write(b);
			}

			iin.close();
			bout.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		return new String(bout.toByteArray());
	}

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException{
		String str = null;
		
//		File file = new File("C:\\CPC\\Jaru\\Software\\Source\\NativeCode\\protocol\\e2\\Debug\\JsonOutGetApp.txt");
//		
//		BufferedReader br = new BufferedReader(new FileReader(file));
//		StringBuffer sb = new StringBuffer(1024);
//		String s;
//		while((s = br.readLine()) != null){
//			sb.append(s);
//			sb.append("\n");
//		}
//		
//		br.close();
//		
//		str = sb.toString();
				
		str="abcderewrwer";
		System.out.println("orignal size:	" + str.getBytes().length);
	
	
		byte[] compData = compData(str);
		System.out.println("compData size: " + compData.length);
		String deCompData = deCompData(compData);
		System.out.println("deCompData size: " + deCompData.getBytes().length);
//		System.out.println(deCompData);
	}

}
